﻿#include "animationManager_Purify.h"


AnimationManager_Purify& AnimationManager_Purify::I(){

    static AnimationManager_Purify instance;
    return instance;
}

AnimationManager_Purify::AnimationManager_Purify()
{
    _state_equ = new AnimationState_Purify();
    _ctrl_equ = new AnimationController_Purify(_state_equ);
    _ctrl_equ->startTimer();

    updateColor(0);


//    execConfig();
}


AnimationManager_Purify::~AnimationManager_Purify(){
    if(_state_equ)  { delete _state_equ; }
    if(_ctrl_equ)   { delete _ctrl_equ; }
}

void AnimationManager_Purify::resetPipe(){
    _state_equ->_pipeGraph->clearPipe();
}

void AnimationManager_Purify::changePipeDiameter(double diam){
    _state_equ->_pipeGraph->diameter = diam;
}

void AnimationManager_Purify::changePPump(int state){
    _state_equ->changePPumpState(state);
}

bool AnimationManager_Purify::checkNullptr(){

    if(_ctrl_equ && _state_equ){
        return true;
    }
    qDebug() << "ERROR: [Class]AnimationManager_Purify [Func]CheckNullPtr - Pointer=NULL";
    return false;
}

void AnimationManager_Purify::setPipeSyringeSpeed(int idx, double speed){

    if(!checkNullptr()){
        return;
    }
    _state_equ->_pipeGraph->changeSyringeSpeed(idx, speed);

}


QVariantMap AnimationManager_Purify::requestInfo(){

    if(!checkNullptr()) return QVariantMap();
    return _ctrl_equ->requestInfo();
}
AnimationCommObject_Purify* AnimationManager_Purify::requestInfo_object(){

    if(!checkNullptr()){
        qDebug() << "[ERROR]CheckNullptrFalse";
        return nullptr;
    }
    return _ctrl_equ->requestInfo_object();
}

void AnimationManager_Purify::reset(){
    if(!checkNullptr()) return;
    _state_equ->reset();
    resetPipe();
}
void AnimationManager_Purify::turnWheel(int num, double speed){
    if(!checkNullptr()) return;
    _state_equ->turnWheel(num, speed);
}
void AnimationManager_Purify::turnWheel_posi(int posi, int direct, double speed){
    if(!checkNullptr()) return;
    _state_equ->turnWheel_posi(posi, direct, speed);
}
void AnimationManager_Purify::turnWheel_posi(int posi, double speed){
    if(!checkNullptr()) return;
    _state_equ->turnWheel_posi(posi, speed);
}
void AnimationManager_Purify::addHat(){
    if(!checkNullptr()) return;
    _state_equ->pushHat();
}
void AnimationManager_Purify::changeValve(int idx, bool state){
    if(!checkNullptr()) return;
    _state_equ->changeValve(idx, state);
}
void AnimationManager_Purify::changeSyringe(int idx, double percentage, double speed){
    if(!checkNullptr()) return;
    _state_equ->changeSyringe(idx, percentage, speed);
}
void AnimationManager_Purify::resetSyringe(int idx){
    if(!checkNullptr()) return;
    double percent = SyringeCalculator::getSyringePercentFromInitialPosition(idx);
    _state_equ->changeSyringe(idx, percent, 1000000);       // 起飞了
}
void AnimationManager_Purify::printCurrentPath(){
    if(!checkNullptr()) return;
    _state_equ->printCurrentPath();
}
bool AnimationManager_Purify::isVertexConnect(int v1, int v2){
    if(!checkNullptr()) return false;
    return _state_equ->isVertexConnect(v1, v2);
}

void AnimationManager_Purify::crashStop(){

}

void AnimationManager_Purify::changeColumn(bool state){
    _state_equ->changeColumn(state);
}

void AnimationManager_Purify::changeDetector(bool state){
    _state_equ->changeDetector(state);
}


void AnimationManager_Purify::updateColor(QString expName){
    _state_equ->setColor(getSyringeColorFromDatabase(expName));
}

void AnimationManager_Purify::changeSpeedUnit(int type){
    _state_equ->changeSpeedUnit(type);
}
int AnimationManager_Purify::getSpeedUnit(){
    return _state_equ->_speedUnit;
}

QMap<int, QColor> AnimationManager_Purify::getSyringeColorFromDatabase(QString expname){

    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql=QString("Select Id from exe_config where value_1='%1' and type='0'").arg(expname);
    query.exec(sql);

    int expId = 1;
    while(query.next()){
        expId = query.value("Id").toInt();
    }
    return getSyringeColorFromDatabase(expId);
}

QMap<int, QColor> AnimationManager_Purify::getSyringeColorFromDatabase(int idx){

    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql = QString("Select value_1, value_2 From exe_config Where Id='%1' and type='1' order by value_1").arg(QString::number(idx));



    QMap<int, QColor> colorList;    // value1 编号 value3颜色



    colorList[0] = QColor(211, 22, 55);
    colorList[1] = QColor(232, 235, 85);
    colorList[2] = QColor(255, 144, 144);
    colorList[3] = QColor(177, 235, 113);
    colorList[4] = QColor(200, 129, 244);
    colorList[5] = QColor(141, 212, 255);
    colorList[6] = QColor(251, 170, 105);

    bool res = query.exec(sql);
    if(!res){
        return colorList;
    }


    while(query.next()){
        colorList.insert(query.value("value_1").toInt(), QColor(query.value("value_2").toString()));
    }
    return colorList;

}


void AnimationManager_Purify::resetWheel(QString consumable){
//    qDebug() << "传入耗材名称" << consumable;
    QString idx = CommonHelper::Common().getItemCode("3", consumable);
//    qDebug() << "获取到耗材编号为" << idx;
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    int wheelIdx = 8;
    QString sql = QString("Select Position from TurnTableInformation where ConsumablesTypes='%1'").arg(idx);
    qDebug() << sql;
    query.exec(sql);
    while(query.next()){
        wheelIdx = query.value("Position").toInt()-1;
    }
//    qDebug() << "得到的Position位置" << wheelIdx;
    turnWheel_posi(wheelIdx);

}

void AnimationManager_Purify::setConsumableName(QString consumableName){
    SyringeCalculator::setConsumableName(consumableName);
}
